package com.tx.websocket.demo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.tx.websocket.demo.common.OffsetBean;
import com.tx.websocket.demo.common.UserLevelEnum;
import com.tx.websocket.demo.dao.ResourceDao;
import com.tx.websocket.demo.dao.UserDao;
import com.tx.websocket.demo.model.BaseResource;
import com.tx.websocket.demo.model.User;
import com.tx.websocket.demo.service.ResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private ResourceDao resourceDao;

    @Resource
    private UserDao userDao;

    @Transactional
    @Override
    public Integer addResources(BaseResource baseResource) {
        return resourceDao.addResources(baseResource);
    }

    @Transactional(readOnly = true)
    @Override
    public List<BaseResource> listResources(BaseResource baseResource, OffsetBean offsetBean) {
        return resourceDao.listResources(baseResource, offsetBean);
    }

    @Transactional(readOnly = true)
    @Override
    public BaseResource getResourcesByPrimaryKey(Long id) {
        return resourceDao.getResourcesByPrimaryKey(id);
    }

    @Transactional
    @Override
    public Integer delResourcesByPrimaryKey(Long id) {
        return resourceDao.delResourcesByPrimaryKey(id);
    }

    @Transactional
    @Override
    public Integer updateResources(BaseResource baseResource) {
        return resourceDao.updateResources(baseResource);
    }

    @Transactional(readOnly = true)
    @Override
    public List<BaseResource> listResByUserId(Long userId) {
        User user = userDao.getUserByPrimaryKey(userId);

        if(user != null && user.getLevel().equals(UserLevelEnum.SUPER_USER.getValue()))
            return resourceDao.listResources(null, null);

        return resourceDao.listResByUserId(userId);
    }

    @Transactional(readOnly = true)
    @Override
    public Integer totalResources(BaseResource baseResource) {
        return resourceDao.totalResources(baseResource);
    }
}
